﻿using Practico2.DataTypes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml.XPath;

namespace Practico2.Business
{
    public class Helper
    {
        public static DTCoordenada DameLatLng(string ubicacion)
        {
            DTCoordenada coord = new DTCoordenada();

            string url = "http://maps.googleapis.com/maps/api/geocode/" + "xml?address=" + ubicacion + "&sensor=false";
            WebResponse resp = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";
                resp = request.GetResponse();
                if (resp != null)
                {
                    XPathDocument doc = new XPathDocument(resp.GetResponseStream());
                    XPathNavigator nav = doc.CreateNavigator();
                    XPathNodeIterator itStatus = nav.Select("/GeocodeResponse/status");
                    while (itStatus.MoveNext())
                    {
                        if (itStatus.Current.Value != "OK")
                        {
                            //Console.WriteLine("Error: '" + itStatus.Current.Value + "'");
                            return null;
                        }
                    }
                    XPathNodeIterator itResult = nav.Select("/GeocodeResponse/result");
                    while (itResult.MoveNext())
                    {
                        XPathNodeIterator itGeom = itResult.Current.Select("geometry");
                        while (itGeom.MoveNext())
                        {
                            XPathNodeIterator itLoc = itGeom.Current.Select("location");
                            while (itLoc.MoveNext())
                            {
                                XPathNodeIterator itLat = itLoc.Current.Select("lat");
                                while (itLat.MoveNext())
                                {
                                    coord.Latitud = itLat.Current.Value;
                                    //Console.WriteLine("lat: " + itLat.Current.Value);
                                }
                                XPathNodeIterator itLng = itLoc.Current.Select("lng");
                                while (itLng.MoveNext())
                                {
                                    coord.Longitud = itLng.Current.Value;
                                    //Console.WriteLine("lng: " + itLng.Current.Value);
                                }
                            }
                        }
                    }
                    return coord;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (resp != null)
                {
                    resp.Close();
                    resp = null;
                }
            }
            return null;
        }

    }
}
